package com.ykh.mybbsdemo.controller;

import com.ykh.mybbsdemo.config.Cache;
import com.ykh.mybbsdemo.config.LogAnnotation;
import com.ykh.mybbsdemo.service.ArticleService;
import com.ykh.mybbsdemo.vo.Result;
import com.ykh.mybbsdemo.vo.params.ArticleParam;
import com.ykh.mybbsdemo.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-08-29
 * Time: 20:39
 */
//json数据进行交互
@RestController
@RequestMapping("articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 首页 文章列表
     * @param pageParams
     * @return
     */
    //自定义一个日志注解相当于 要对此接口进行记录日志
    @LogAnnotation(module = "文章", operation = "获取文章列表")
    //请求是POST类型的,所以用PostMapping
    @PostMapping
//    @Cache(expire = 5 * 60 * 1000,name = "article_list")
    public Result listArticle(@RequestBody PageParams pageParams) {
        return articleService.listArticle(pageParams);
    }

    /**
     * 最热文章
     * @param
     * @return
     */
    //请求是POST类型的,所以用PostMapping
    @PostMapping("hot")
    @Cache(expire = 5 * 60 * 1000,name = "hot_article")
    public Result hotArticle() {
        int limit = 5;
        return articleService.hotArticle(limit);
    }

    /**
     * 最新文章
     * @param
     * @return
     */
    //请求是POST类型的,所以用PostMapping
    @PostMapping("new")
    @Cache(expire = 5 * 60 * 1000,name = "new_article")
    public Result newArticles() {
        int limit = 5;
        return articleService.newArticles(limit);
    }

    /**
     * 文章归档
     */
    @PostMapping("listArchives")
    public Result listArchives(@RequestBody(required = false) PageParams pageParams) {
        return articleService.listArchives();
    }

    /**
     * 查看文章详情
     * @param articleId
     * @return
     */
    @PostMapping("view/{id}")
    public Result findArticleById(@PathVariable("id")long articleId) {
        return articleService.findArticleById(articleId);
    }

    /**
     * 发布文章
     */
    @PostMapping("publish")
    public Result publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);
    }
}
